package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dao.CommentDao;
import util.DBUtil;
import entity.Comment;

public class CommentDaoJdbcImpl implements CommentDao {

	@Override
	public void add(Comment c) throws Exception {
		String sql="insert into friend_comment (username,date,title,content) " +
				"values(?,?,?,?)";
		Connection con=DBUtil.openInThread();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setString(1, c.getUsername());
		pst.setDate(2, c.getDate());
		pst.setString(3, c.getTitle());
		pst.setString(4, c.getContent());
		pst.execute();
		pst.close();
		DBUtil.closeInThread();
	}

	@Override
	public List<Comment> findAll() throws Exception {
		List<Comment> comments= new ArrayList<Comment>();
		String sql="select * from friend_comment";
		Connection con=DBUtil.openInThread();
		Statement stm=con.createStatement();
		ResultSet rs=stm.executeQuery(sql);
		while(rs.next()){
			String username=rs.getString("username");
			Date date = rs.getDate("date");
			String title=rs.getString("title");
			String content=rs.getString("content");
			Comment c = new Comment(username, date, title, content);
			comments.add(c);
		}
		return comments;
	}

}
